package com.masterclient.app.modules.aftersale;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.masterclient.app.R;
import com.eoner.baselibrary.bean.aftersale.ShipmentsBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：sean
 * 创建时间：2017/8/27 16:57
 * 功能说明：
 */

public class ShipmentDialog extends Dialog{
    List<ShipmentsBean.DataBean.ShShipmentCompanysBean> list;
    public ShipmentDialog(@NonNull Context context, List<ShipmentsBean.DataBean.ShShipmentCompanysBean> list) {
        super(context, R.style.MyDialog);

        if (list == null) {
            this.list = new ArrayList<>();
        }else {
            this.list = list;
        }
    }

    private TextView tv_cancle,tv_confirm;
    private LoopView loopView;
    private String companyName;
    private ShipmentsBean.DataBean.ShShipmentCompanysBean dataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_return_reason);

        tv_cancle = (TextView) findViewById(R.id.tv_cancle);
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        loopView = (LoopView) findViewById(R.id.loopView);
        List<String> companys = new ArrayList<>();
        for(ShipmentsBean.DataBean.ShShipmentCompanysBean data:list){
            companys.add(data.getSh_name());
        }
        companyName = companys.get(0);
        dataBean = list.get(0);
        loopView.setItems(companys);

        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                companyName = companys.get(index);
                dataBean = list.get(index);
            }
        });

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onConfirmListener!=null){
                    onConfirmListener.onConfirm(companyName, dataBean.getSh_code());
                }
                dismiss();
            }
        });

    }


    public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
    }

    private OnConfirmListener onConfirmListener;

    public interface OnConfirmListener{
        public void onConfirm(String company, String code);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        WindowManager.LayoutParams windowparams = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }
}
